package com.common.utils;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.HashMap;

/**
 * 类型转换工具类
 *
 * @author xin.ding
 * @date 2019/12/13 16:23
 */
public class TableTypeUtils {
    private TableTypeUtils() {
        throw new UnsupportedOperationException();
    }


    public static final String CHAR = "char";
    public static final String VARCHAR = "varchar";
    public static final String STRING = "string";
    public static final String NCHAR = "nchar";
    public static final String NVARCHAR = "nvarchar";
    public static final String INT = "int";
    public static final String INTEGER = "integer";
    public static final String INTERVAL = "interval";
    public static final String BIGINT = "bigint";
    public static final String LONG = "long";
    public static final String LONGINT = "longint";
    public static final String TINYINT = "tinyint";
    public static final String SMALLINT = "smallint";
    public static final String REAL = "real";
    public static final String FLOAT = "float";
    public static final String DOUBLE = "double";
    public static final String NUMBER = "number";
    public static final String NUMERIC = "numeric";
    public static final String DECIMAL = "decimal";
    public static final String BIGDECIMAL = "bigdecimal";
    public static final String DATE = "date";
    public static final String TIMESTAMP = "timestamp";
    public static final String TIME = "time";
    public static final String BOOLEAN = "boolean";
    public static final String RAW = "raw";
    public static final String BLOB = "blob";
    public static final String CLOB = "clog";
    public static final String ARRAY_REGEX = "array\\(\\w+\\)";
    public static final String MAP = "map";
    public static final String OBJECT = "object";


    /**
     * 数据库字段定义类型转化为对应Java数据类型
     *
     * @param type 数据库字段类型
     * @return Java对应类型
     */
    public static Class<?> stringConvertClass(String type) {
        type = type.toLowerCase();

        if (type.startsWith(CHAR) || type.startsWith(VARCHAR) || type.startsWith(STRING)
                || type.startsWith(NCHAR) || type.startsWith(NVARCHAR)) {
            return String.class;
        } else if (type.startsWith(INT) || type.startsWith(INTEGER) || type.startsWith(INTERVAL)) {
            return Integer.class;
        } else if (type.startsWith(BIGINT) || type.startsWith(LONG) || type.startsWith(LONGINT)) {
            return Long.class;
        } else if (type.startsWith(TINYINT)) {
            return Byte.class;
        } else if (type.startsWith(SMALLINT)) {
            return Short.class;
        } else if (type.startsWith(REAL) || type.startsWith(FLOAT)) {
            return Float.class;
        } else if (type.startsWith(DOUBLE) || type.startsWith(NUMBER) || type.startsWith(NUMERIC)) {
            return BigDecimal.class;
        } else if (type.startsWith(DECIMAL) || type.startsWith(BIGDECIMAL)) {
            return BigDecimal.class;
        } else if (type.startsWith(DATE)) {
            return Date.class;
        } else if (type.startsWith(TIMESTAMP)) {
            return Timestamp.class;
        } else if (type.startsWith(TIME)) {
            return Time.class;
        } else if (type.startsWith(BOOLEAN)) {
            return Boolean.class;
        } else if (type.startsWith(RAW)) {
            return byte[].class;
        } else if (type.startsWith(BLOB)) {
            return java.sql.Blob.class;
        } else if (type.startsWith(CLOB)) {
            return java.sql.Clob.class;
        } else if (type.matches(ARRAY_REGEX)) {
            String arrayType = type.substring(type.indexOf('(') + 1, type.lastIndexOf(')'));
            switch (arrayType) {
                case LONG:
                    return Long[].class;
                case STRING:
                    return String[].class;
                case MAP:
                    return HashMap[].class;
                default:
            }
        } else if (type.startsWith(OBJECT)) {
            return java.lang.Object.class;
        }

        throw new RuntimeException("不支持 " + type + " 类型");
    }
}
